<?php

namespace App\Http\Admin\Controllers\User;
use App\Requests\StaffRequest;
use App\Services\user\UserInfoServices;
use App\Http\Controllers\Controller;
use Hlcreation\Basic\Helper;
/**
 *   副管理信息
 * */

class UserInfoController  extends Controller
{

    /**
     * @param UserInfoServices $services
     */
    public function __construct(UserInfoServices $services)
    {

        $this->services = $services;
    }



    /**
     * 副管理列表
     */
    public function ls(){

        $post['limit']=  Helper::getPost('limit',20);
        $name=  Helper::getPost('name','');
        $post['page']=Helper::getPost('page', 1);
        $create_time=Helper::getPost('create_time','');

        $where = [];
        if ($name){
            $where = ['account'=>$name];
        }
        if ($create_time){

            $start_time = $create_time.' 00:00:00';
            $end_time = $create_time.' 23:59:59';


//            $where[] = ['created_at','between',[$start_time,$end_time]];
            $where[] = ['created_at', '>=',$start_time];
            $where[] = ['created_at', '<',$end_time];
            //dd($map);
        }


        $list = $this->services->UserList($where,$post);

        return Helper::toJson("副管理列表", 200, $list);
    }



    /**
     *  添加员工
     * */
    public function add(StaffRequest $request){


        $all = $request->all();

        $userInfo = Helper::getPost('userinfo');
        $all['creator_uid']=$userInfo['user_info']['id'];


        $data = $this->services->StaffSave($all);
        if($data){
            return Helper::toJson("创建成功", 200);
        }else{
            return Helper::toJson("创建失败", 400);
        }


    }

    /**
     *  查看员工信息
     *
     * */
    public function infos(){

        $sraff_id=Helper::getPost('staff_id');
        $data = $this->services->getStaff($sraff_id);
        return Helper::toJson("数据获取成功", 200,$data);
    }


    /**
     *  编辑员工
     *
     * */
    public function edit(StaffRequest $request){

        $staff_id=Helper::getPost('staff_id');
       // $user_id=Helper::getPost('user_id');
        $all = $request->all();
        $userInfo = Helper::getPost('userinfo');
        $all['creator_uid']=$userInfo['user_info']['id'];

        $data = $this->services->StaffUpdate($staff_id,$all);

        if($data){
            return Helper::toJson("修改成功", 200);
        }else{
            return Helper::toJson("修改失败", 400);
        }


    }

    //禁用员工
    public  function  editDisabled(){
        $admin_id=Helper::getPost('userinfo')['user_info']['id'];
        $user_id=Helper::getPost('user_id');
        $status= Helper::getPost('status');
        if($status == 0){
            $this->services->EditDisabled($admin_id,$user_id,0);
            return Helper::toJson("启用成功", 200);
        }else{
            $this->services->EditDisabled($admin_id,$user_id,1);
            return Helper::toJson("禁用成功", 200);
        }

    }

    /**
     *
     *  删除员工
     * */
    public function staffDel(){
        $staff_id=Helper::getPost('staff_id');
        /*$user_id=Helper::getPost('user_id');*/
        $admin_id=Helper::getPost('userinfo')['user_info']['id'];
        $data = $this->services->StaffDelte($admin_id,$staff_id);
        if($data) return Helper::toJson("删除成功", 200);
        else Helper::toJson("删除失败", 400);
    }











}
